<?php
namespace App\Models;

use Validator;

class Printer extends BaseModel
{
    protected $table = 'printer';

    protected $rules = [
        'serial_number' => 'required|digits:9',
        'auth_code' => 'required'
    ];

    protected $message = [
        'serial_number.required' => '打印机编号必须',
        'serial_number.digits' => '打印机编号必须为9位数的数字',
        'auth_code.required' => '打印机识别码不能为空',
    ];

    public function __construct()
    {
        parent::__construct();
    }

    public function getPrinterList()
    {
        $pageSize = 15;
        $pageLength = 10;
        if(plat_identify() == 'admin') {
            if (is_administrator()) {
                $where = [];
            } else {
                $where['admin_id'] = plat_admin_id();
            }
        } else {
            $where['shop_id'] = session('account')['shop_id'];
        }

        $rows = $this->multiSelect("*")
            ->multiWhere($where)
            ->multiOrder(['id' => 'asc'])
            ->paginate($pageSize);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = $this->getPageNoList($rows['last_page'], request('page', 1), $pageLength);
        }

        $txtStr['status'] = [0 => '已禁用', 1 => '正常'];
        $rows['data'] = int_to_string($rows['data'], $txtStr);
        return $rows;
    }

    public function addOrUpdate($admin_id, $shop_id)
    {
        if (!($admin_id && $shop_id)) {
            $this->error = '非法操作';
            return false;
        }
        $time = time();
        $data['admin_id'] = $admin_id;
        $data['shop_id'] = $shop_id;
        $data['serial_number'] = trim(request('serial_number'));
        $data['auth_code'] = trim(request('auth_code'));
        $data['desc'] = trim(request('desc'));
        $data['mobile'] = trim(request('mobile'));

        $validator = Validator::make($data, $this->rules,$this->message);
        //验证失败
        if ($validator->fails()) {
            //返回所有错误信息
            $this->error = $validator->errors()->first();
            return false;
        }

        $supportPrinter = new \App\Support\Printer();
        $printerContent = $data['serial_number'].'#'.$data['auth_code'];
        $printerStatus = $supportPrinter->queryPrinterStatus($data['serial_number']);
        // 判断打印机是否已经添加到云端
        if($printerStatus == 0) {
            $result = $supportPrinter->addPrinter($printerContent);
            if($result) {
                $data['is_add'] = 1;
            }
        } else {
            $data['is_add'] = 1;
        }

        if (request('id')) {
            $data['id'] = request('id');
            $data['updated_at'] = date('Y-m-d H:i:s',$time);
        } else {
            $data['addtime'] = $time;
            $data['created_at'] = date('Y-m-d H:i:s',$time);
        }
        $this->_start();
        try{
            $this->_save($data);
            $this->_commit();
            return true;
        }catch(\Exception $e){
            $this->_rollBack();
            $this->error = request('id') ? '编辑打印机失败' : '新增打印机失败';
            return false;
        }

    }

}